package com.redsoft.epip.service;

import java.util.List;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.redsoft.spirit.dao.AccountRepository;
import com.redsoft.spirit.entity.Account;
import com.redsoft.spirit.service.IAccountService;
import com.redsoft.spirit.util.BeanUtils;

/**
 * 后台用户管理的Service.
 * 
 * <pre>
 * 后台用户管理的Service.
 * </pre>
 * 
 * @author
 * @since 1.0
 */
@Service
@Transactional
public class AccountServiceImpl implements IAccountService {

	@Autowired
	private AccountRepository accountRepository;

	/**
	 * 实现接口里的登录方法
	 * 
	 * <pre>
	 * 实现接口里的登录方法
	 * </pre>
	 * 
	 * @see com.redsoft.spirit.service.IAccountService#login(java.lang.String,
	 *      java.lang.String)
	 */
	@Override
	public Account login(String userName, String password, int logonType) throws Exception {
		try {
			Account account = accountRepository.getByAccountName(userName);
			if (account == null) {
				throw new RuntimeException("帐号不存在");
			}
			String secretPwd = new Md5Hash(password, "meiyoumima").toString();

			if (!secretPwd.equals(account.getAccountPwd())) {
				throw new RuntimeException("密码不正确");
			}

			if (account.getIsActive().equals("0")) {
				throw new RuntimeException("帐号审核中");
			}

			if (account.getIsActive().equals("-1")) {
				throw new RuntimeException("帐号被冻结");
			}

			/*
			 * 将持久化的类进行深拷贝，避免更新到数据库
			 */
			Account accountTmp = new Account();
			BeanUtils.copyBean2Bean(accountTmp, account);
			;
			// accountTmp.setAvatar(QiNiuUtil.getDownloadImgUrl(account.getAvatar()));

			return accountTmp;
		} catch (Exception e) {
			throw e;
		}
	}

	/**
	 * 修改用户密码
	 * 
	 * <pre>
	 * 修改用户密码
	 * </pre>
	 * 
	 * @author 李晓东
	 * @param account
	 * @throws Exception
	 */
	public void updatePassword(Account account) throws Exception {
		Account oldTmp = accountRepository.getById(account.getId());

		/*
		 * oldTmp是持久化状态，不用再次调用save方法
		 */
		// BeanUtils.copyBeanNotNull2Bean(account, oldTmp);
		oldTmp.setAccountPwd(account.getAccountPwd());

		// accountRepository.save(oldTmp);
	}

	/**
	 * 修改用户头像
	 * 
	 * <pre>
	 * 修改用户头像
	 * </pre>
	 * 
	 * @author 李晓东
	 * @param account
	 * @throws Exception
	 */
	public void updateUserPic(Account account) throws Exception {
		Account oldTmp = accountRepository.getById(account.getId());

		/*
		 * oldTmp是持久化状态，不用再次调用save方法
		 */
		// BeanUtils.copyBeanNotNull2Bean(account, oldTmp);
		oldTmp.setAvatar(account.getAvatar());

		// accountRepository.save(oldTmp);
	}

	/**
	 * 根据ID获取用户信息
	 * 
	 * <pre>
	 * 根据ID获取用户信息
	 * </pre>
	 * 
	 * @author 李晓东
	 * @param id
	 * @return
	 */
	public Account getById(String id) {
		return accountRepository.getById(id);
	}

	/**
	 * 获取所有用户
	 * 
	 * <pre>
	 * 获取所有用户
	 * </pre>
	 * 
	 * @author 李晓东
	 * @return
	 */
	List<Account> findAll() {
		return accountRepository.findAll();
	}

	/**
	 * 根据userName获取用户信息
	 * 
	 * <pre>
	 * 根据userName获取用户信息
	 * </pre>
	 * 
	 * @author 李晓东
	 * @param userName
	 * @return
	 */
	public Account getByAccountName(String userName) {
		return accountRepository.getByAccountName(userName);
	}

}
